package cn.sfa.controller.news;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.sfa.common.jms.ResourceMessageProducer;
import cn.sfa.common.pagination.Pagination;
import cn.sfa.common.utils.CommonUtil;
import cn.sfa.common.utils.FilterSpesChar;
import cn.sfa.common.utils.WebInitBinderContoller;
import cn.sfa.entity.affixItem.SaleAffixItem;
import cn.sfa.entity.meeting.SaleMeeting;
import cn.sfa.entity.news.MonthBusiness;
import cn.sfa.entity.news.SaleNews;
import cn.sfa.entity.news.SaleNewsSee;
import cn.sfa.entity.users.SaleUsersLogin;
import cn.sfa.service.affix.AffixService;
import cn.sfa.service.agentinfo.AgentInfoService;
import cn.sfa.service.meeting.MeetingService;
import cn.sfa.service.news.PreNewsService;
import cn.sfa.service.product.ProductService;

@Controller
@RequestMapping("/news/index")
public class PreNewsController extends WebInitBinderContoller {

	Logger log = LoggerFactory.getLogger(PreNewsController.class);

	@Autowired
	private PreNewsService newsService;

	@Autowired
	private MeetingService meetingService;

	@Autowired
	private ProductService productService;
	@Autowired
	private AffixService affixService;
	
	@Autowired
	AgentInfoService agentInfoService;
	@Autowired
	ResourceMessageProducer producer;
	
	@RequestMapping
	public String index(HttpServletRequest request, ModelMap map,SaleNews saleNews) throws Exception {
		String path = request.getContextPath();
		  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
		  Pagination pagination = newsService.searchNews(
					1,10,"" ,saleNews,null,null,null); 
		  map.put("noticePagination", pagination);
		return "/home";
	}

	/*
	 * 本月所有义务总数
	 */
	@RequestMapping("getBusiness")
	public void getBusiness(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {

		SaleUsersLogin user = (SaleUsersLogin) request.getSession()
				.getAttribute("user");
		MonthBusiness business = newsService.getThisMonthBusinessAll(user
				.getUserAgentId());// 本月所有业务数

		StringBuffer result = new StringBuffer();

		result.append("{"); 
		result.append("THISMONTHUNDERWRITEEDS:"+business.getTHISMONTHUNDERWRITEEDS()+",");
		result.append("THISMONTHNOUNDERWRITEEDS:"+business.getTHISMONTHNOUNDERWRITEEDS()+",");
		result.append("THISMONTHNOTEEDS:"+business.getTHISMONTHNOTEEDS()+",");
		result.append("THISMONTHNONOTEEDS:"+business.getTHISMONTHNONOTEEDS()+",");
		result.append("THISMONTHRECEIPTEDS:"+business.getTHISMONTHRECEIPTEDS()+",");
		result.append("THISMONTHNORECEIPTEDS:"+business.getTHISMONTHNORECEIPTEDS()+",");
		result.append("THISMONTHSURRENDEREDS:"+business.getTHISMONTHSURRENDEREDS()+",");
		result.append("THISMONTHHEAREDS:"+business.getTHISMONTHHEAREDS()); 
		result.append("}");
		
		request.getSession().setAttribute("THISMONTHUNDERWRITEEDS", business.getTHISMONTHUNDERWRITEEDS());
		request.getSession().setAttribute("THISMONTHNOUNDERWRITEEDS", business.getTHISMONTHNOUNDERWRITEEDS());
		request.getSession().setAttribute("THISMONTHNOTEEDS", business.getTHISMONTHNOTEEDS());
		request.getSession().setAttribute("THISMONTHNONOTEEDS", business.getTHISMONTHNONOTEEDS());
		request.getSession().setAttribute("THISMONTHRECEIPTEDS", business.getTHISMONTHRECEIPTEDS());
		request.getSession().setAttribute("THISMONTHNORECEIPTEDS", business.getTHISMONTHNORECEIPTEDS());
		request.getSession().setAttribute("THISMONTHSURRENDEREDS", business.getTHISMONTHSURRENDEREDS());
		request.getSession().setAttribute("THISMONTHHEAREDS", business.getTHISMONTHHEAREDS());

		PrintWriter out = response.getWriter();
		response.setContentType("text/html");
		out.print(result.toString());
		out.flush();
		out.close();
	}
	/*
	 * 本月所有义务总数具体都那个业务
	 */
	@RequestMapping("/getSpBusiness")
	public String getSpecificBusiness(
			@RequestParam(value = "type", required = false) String type,
			HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		SaleUsersLogin user = (SaleUsersLogin) request.getSession().getAttribute("user");
		String agentid = user.getUserAgentId();
		String agentname = user.getUserName();
		
		String url = request.getContextPath()
		+ "/news/index/getSpBusiness?type=" + type + "";

		int currentPage = 1;
		if (!CommonUtil.isEmpty(request.getParameter("currentPage"))) {
			currentPage = Integer.parseInt(request.getParameter("currentPage"));
		}
		Pagination pagination = newsService.getThisMonthBusiness(currentPage, 10, url, agentid, type);
		String THISMONTHUNDERWRITEEDS = request.getSession().getAttribute("THISMONTHUNDERWRITEEDS").toString();
		String THISMONTHNOUNDERWRITEEDS = request.getSession().getAttribute("THISMONTHNOUNDERWRITEEDS").toString();
		String THISMONTHNOTEEDS = request.getSession().getAttribute("THISMONTHNOTEEDS").toString();
		String THISMONTHNONOTEEDS = request.getSession().getAttribute("THISMONTHNONOTEEDS").toString();
		String THISMONTHRECEIPTEDS = request.getSession().getAttribute("THISMONTHRECEIPTEDS").toString();
		String THISMONTHNORECEIPTEDS = request.getSession().getAttribute("THISMONTHNORECEIPTEDS").toString();
		String THISMONTHSURRENDEREDS = request.getSession().getAttribute("THISMONTHSURRENDEREDS").toString();
		String THISMONTHHEAREDS = request.getSession().getAttribute("THISMONTHHEAREDS").toString();
		
		map.put("type", type);
		map.put("agentname", agentname);
		map.put("pagination", pagination);
		map.put("THISMONTHUNDERWRITEEDS", THISMONTHUNDERWRITEEDS);
		map.put("THISMONTHNOUNDERWRITEEDS", THISMONTHNOUNDERWRITEEDS);
		map.put("THISMONTHNOTEEDS", THISMONTHNOTEEDS);
		map.put("THISMONTHNONOTEEDS", THISMONTHNONOTEEDS);
		map.put("THISMONTHRECEIPTEDS", THISMONTHRECEIPTEDS);
		map.put("THISMONTHNORECEIPTEDS", THISMONTHNORECEIPTEDS);
		map.put("THISMONTHSURRENDEREDS", THISMONTHSURRENDEREDS);
		map.put("THISMONTHHEAREDS", THISMONTHHEAREDS);
		return "/business/thismonthbusinessall";
	}

	@RequestMapping("/view/{saleNewsId}")
	public String view(@PathVariable("saleNewsId")String saleNewsId,
			ModelMap map,HttpServletRequest request){
		SaleUsersLogin user=(SaleUsersLogin)request.getSession().getAttribute("user");
		SaleNewsSee newsSee=new SaleNewsSee();
		newsSee.setNewsId(saleNewsId);
		newsSee.setCreateUid(user.getSaleUsersLoginId());
//		boolean flag=newsService.newsCount(newsSee);
		 
		SaleNews news=newsService.getNew(saleNewsId );
		news.setNewsContent(news.getNewsContent().replace("&amp;lt;", "&lt;").replace("&amp;gt;", "&gt;"));
		map.put("new", news);
		//附件列表
		List<SaleAffixItem> affixList = affixService.getAffix(
				"SALENEWS", saleNewsId);
		map.put("affixList", affixList);
		return "/news/view";
	}
	
	@RequestMapping(value="/more")
	public String more(@RequestParam(value="currentPage", required=false) String currentPage,
			@RequestParam(value="beginCreateDate", required=false)String beginCreateDate,
			@RequestParam(value="endCreateDate", required=false)String endCreateDate,
			@RequestParam(value="newsTitle", required=false)String newsTitle,
			@RequestParam(value="type", required=false)String type,
			HttpServletRequest request,ModelMap map,SaleNews saleNews)
	{
		
SaleUsersLogin user=(SaleUsersLogin)request.getSession().getAttribute("user");
		
		int pageIndex;
		try{
			pageIndex = Integer.valueOf(currentPage);
		}catch(Exception ex){
			pageIndex = 1;
		} 
		
		Pagination pagination=null;
		pagination = newsService.searchNews(
				pageIndex,10,request.getContextPath()+"/manage/news/index/getnews?" +
						"&newsTitle="+CommonUtil.jugeAndFixNull(saleNews.getNewsTitle())+
						"&newsContent="+CommonUtil.jugeAndFixNull(saleNews.getNewsContent())+
						"&beginCreateDate="+(CommonUtil.isEmpty(beginCreateDate) ? "" :  beginCreateDate)+
						"&endCreateDate="+(CommonUtil.isEmpty(endCreateDate) ? "" :  endCreateDate)
						,saleNews,beginCreateDate,endCreateDate,user); 
		 
		map.put("pagination", pagination);
		map.put("newsTitle",saleNews.getNewsTitle());
		map.put("newsContent",saleNews.getNewsContent());
		map.put("beginCreateDate", CommonUtil.isEmpty(beginCreateDate) ? ""
				:  beginCreateDate);
		map.put("endCreateDate", CommonUtil.isEmpty(endCreateDate) ? ""
				:  endCreateDate);
		map.put("page",pageIndex); 
 
		return "/news/more";
	}
	
	@RequestMapping(value = "/meetingmore")
	public String meetingMore(
			@RequestParam(value = "currentPage", required = false) String currentPage,
			@RequestParam(value = "isInner", required = false) String isInner,
			HttpServletRequest request, ModelMap map) {
		
		int pageIndex;
		try {
			pageIndex = Integer.valueOf(currentPage);
		} catch (Exception ex) {
			pageIndex = 1;
		}

		Pagination pagination = meetingService.searchMeetingsForMore(pageIndex, 10, request.getContextPath() + "/news/index/meetingmore",isInner);
		request.setAttribute("pagination", pagination);
		map.put("page", pageIndex);

		return "/meeting/more";
	}
}
